package org.ru.mse10.cvis.entity.codelists;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.MapKey;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.ru.mse10.cvis.constants.CodeValueQueries;
import org.ru.mse10.cvis.entity.misc.Identity;
import org.ru.mse10.cvis.entity.misc.TranslatedValue;
import org.ru.mse10.cvis.entity.util.Bidirectional;

/**
 * The Class Codelist.
 */
@Entity
@Table(name = "codelist")
@NamedQueries(value = { @NamedQuery(name = CodeValueQueries.QUERY_CODELIST_BY_CODE_KEY, query = CodeValueQueries.QUERY_CODELIST_BY_CODE) })
public class Codelist extends Identity implements Bidirectional {

	/** The code. */
	private String code;

	/** The translated description. */
	private Map<String, TranslatedValue> translatedDescription = new HashMap<String, TranslatedValue>();

	/** The codelist values. */
	private List<CodelistValue> codelistValues = new LinkedList<CodelistValue>();

	/**
	 * Gets the code.
	 * 
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * Sets the code.
	 * 
	 * @param code
	 *            the new code
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * Gets the translated description.
	 * 
	 * @return the translated description
	 */
	@ManyToMany(cascade = CascadeType.ALL)
	@MapKey(name = "languageKey")
	public Map<String, TranslatedValue> getTranslatedDescription() {
		return translatedDescription;
	}

	/**
	 * Gets the translated descriptions.
	 * 
	 * @return the translated descriptions
	 */
	@Transient
	public List<TranslatedValue> getTranslatedDescriptions() {
		return new ArrayList<TranslatedValue>(translatedDescription.values());
	}

	/**
	 * Sets the translated description.
	 * 
	 * @param translatedDescription
	 *            the translated description
	 */
	public void setTranslatedDescription(
			Map<String, TranslatedValue> translatedDescription) {
		this.translatedDescription = translatedDescription;
	}

	/**
	 * Gets the codelist values.
	 * 
	 * @return the codelist values
	 */
	@OneToMany(orphanRemoval = true, cascade = CascadeType.ALL, mappedBy = "codelist")
	public List<CodelistValue> getCodelistValues() {
		return codelistValues;
	}

	/**
	 * Sets the codelist values.
	 * 
	 * @param codelistValues
	 *            the new codelist values
	 */
	public void setCodelistValues(List<CodelistValue> codelistValues) {
		this.codelistValues = codelistValues;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.ru.mse10.cvis.entity.util.Bidirectional#initializeBidirectionalReferences
	 * ()
	 */
	@Override
	public void initializeBidirectionalReferences() {
		for (CodelistValue element : getCodelistValues()) {
			element.setCodelist(this);
		}
	}
}
